package org.qydjk.common.util;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

import java.util.*;

/**
 * list/数组相关处理
 */
public class ListUtil {
    /**
     * 两层数组去除一层
     * @param list 原数组[[11],[33],[70]]
     * @return 原数组[11,33,70]
     */
    public static List<Object> wipeInnerList(List<List<Object>> list){
        List<Object> objs = new ArrayList<Object>();
        for (int i = 0;i < list.size();i++){
            List<Object> ilist = list.get(i);
            if(ilist != null && ilist.size() == 1){
                objs.add(ilist.get(0));
            }
        }
        return objs;
    }


    /**
     * 数组是不是空
     * @param list
     * @return
     */
    public static boolean isNullOrEmpty(Collection list){
        if(list == null || list.size() <= 0){
            return true;
        }
        return false;
    }

    /**
     * 数组去重，主要用于字符串和整数
     * @param list
     * @return
     */
    public static String wipeRepeat(Object[] list){
        Set<String> set = new HashSet<String>();
        for(int i = 0;i < list.length;i++){
            set.add(list[i].toString());
        }
        return StringUtils.join(set.toArray(),",");
    }

    /**
     * 数组转字符串
     * @param list
     * @return
     */
    public static String arrToString(Object[] list){
        return StringUtils.join(list,",");
    }

    /**
     * 两个数组合并
     * @param arr1
     * @param arr2
     * @return
     */
    public static Object[] arrCombine(Object[] arr1,Object[] arr2){
        Object[] both = (String[]) ArrayUtils.addAll(arr1, arr2);
        return both;
    }
}
